package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.AtRecord;
import cn.wolfcode.trip.base.mapper.AtRecordMapper;
import cn.wolfcode.trip.base.query.AtQueryObject;
import cn.wolfcode.trip.base.service.IAtSomeOneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * at记录的业务层
 *
 * @author Wangyk
 * @date 2019/1/17 18:59
 */
@Service
public class AtSomeOneServiceImpl implements IAtSomeOneService {
    @Autowired
    private AtRecordMapper atRecordMapper;

    /**
     * 添加一条At记录
     *
     * @param atRecord at记录数据
     */
    @Override
    public void addAAtRecord(AtRecord atRecord) {
        //1:设置时间
        atRecord.setAtTime(new Date());

        //2:添加记录
        atRecordMapper.insert(atRecord);
    }

    /**
     * 通过查询条件查询at记录
     *
     * @param qo 查询条件
     * @return at记录集合
     */
    @Override
    public PageInfo getAtRecordsByQueryObject(AtQueryObject qo) {
        //1:查询记录
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<AtRecord> atRecordList = atRecordMapper.selectByQueryObject(qo);
        PageInfo<AtRecord> atRecordPageInfo = new PageInfo<>(atRecordList);
        //2:更改未读状态
        if (atRecordList.size() > 0) {
            try {
                atRecordMapper.updateHasReadByUserList(atRecordList);
            } catch (Exception e) {
            }
        }
        return atRecordPageInfo;
    }
}
